<?php
/**
 * User: @lose
 * Date: 2019-10-14
 * Time: 11:47
 */

namespace Biz\service\admin;

use Biz\model\CategoryWikiCard as mCategoryWikiCard;
use Biz\model\ShopCategoryClassify as mShopCategoryClassify;
use Biz\model\wiki\Wiki as mWiki;
use Doraemon\service\User as sUser;


class CategoryWikiCard
{
    public static function getList($where, $limit)
    {
        $batchUserInfo = [];
        $list = mCategoryWikiCard::getListByWhere($where, $limit, ['id' => 'DESC']);
        $wikiList = mWiki::selectByWhere(['id' => array_column($list, 'wiki_id')]);
        $wikiList = array_column($wikiList, null, 'id');
        $brandId = array_column($list, 'brand_id');
        $categoryList = mShopCategoryClassify::getListByIds(array_column($list, 'category_id'));
        if ($brandId) {
            $batchUserInfo = sUser::getBatchBasicUserInfo($brandId);
            $batchUserInfo = array_column($batchUserInfo, null, 'uid');
        }

        foreach ($list as &$info) {
            if (isset($wikiList[$info['wiki_id']])) {
                $info['title'] = $wikiList[$info['wiki_id']]['title'];
            }

            if (isset($batchUserInfo[$info['brand_id']])) {
                $info['brand_name'] = $batchUserInfo[$info['brand_id']]['nick'];
            }

            if (isset($categoryList[$info['category_id']])) {
                $info['classify_name'] = $categoryList[$info['category_id']]['classify_name'];
            }
        }

        return ['list' => $list, 'total' => mCategoryWikiCard::getCount($where)];
    }

    public static function update($params, $adminId)
    {
        self::checkParams($params);

        $data = [
            'wiki_id' => $params['wiki_id'],
            'brand_id' => $params['brand_id'],
            'category_id' => $params['category_id'],
            'insert_position' => $params['insert_position'] + 1,
            'start_time' => $params['start_time'],
            'end_time' => $params['end_time'],
            'addtime' => date('Y-m-d H:i:s'),
            'edittime' => date('Y-m-d H:i:s'),
            'card_status' => mCategoryWikiCard::STATUS_NORMAL,
            'admin_id' => $adminId
        ];

        return mCategoryWikiCard::addData($data);
    }

    public static function delete($id)
    {
        return mCategoryWikiCard::updateById($id);
    }

    private static function checkParams($params)
    {
        $userInfo = sUser::getBasicUserInfo($params['brand_id']);
        if ($userInfo['type'] != 1) {
            throw new \Exception('非品牌用户');
        }
        if (!$params['wiki_id']) {
            throw new \Exception('wiki不存在');
        }

        $wikiInfo = mWiki::getInfoByWikiId($params['wiki_id']);
        if (!$wikiInfo) {
            throw new \Exception('wiki不存在');
        }

        $categoryInfo = mShopCategoryClassify::getInfo($params['category_id']);
        if ($categoryInfo['deep'] != mShopCategoryClassify::DEEP_FIRST_CLASSIFY) {
            throw new \Exception('只能输入一级分类');
        }

        if ($categoryInfo['status'] != mShopCategoryClassify::STATUS_NORMAL) {
            throw new \Exception('分类不存在或处于隐藏状态');
        }

        $list = mCategoryWikiCard::getListByWhere([
            'category_id' => $params['category_id'],
            'insert_position' => $params['insert_position']
        ]);

        $check = true;
        $id = 0;
        foreach ($list as $info) {
            if ($params['start_time'] >= $info['start_time'] && $params['start_time'] <= $info['end_time']) {
                $check = false;
                $id = $info['id'];
                break;
            }

            if ($params['end_time'] >= $info['start_time'] && $params['end_time'] <= $info['end_time']) {
                $check = false;
                $id = $info['id'];
                break;
            }
        }

        if ($check === false) {
            throw new \Exception('该时间段已经存在卡片 id为' . $id);
        }
    }

    public static function getBasicInfo($objType, $objId)
    {
        if ($objType == 'wiki') {
            $info = mWiki::getByWhere(['id' => $objId]);
            return $info ? $info['title'] : 'wiki已下线或不存在';
        } else if ($objType = 'brand') {
            $info = sUser::getBasicUserInfo($objId);
            return $info ? $info['nick'] : '用户不存在或非品牌用户';
        }
        return '';
    }
}